﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;


namespace pb_ws_client
{

    public partial class main : Form
    {
        WebServiceReference.AppServiceSoapClient wsr;

        public main()
        {
            InitializeComponent();
           
            wsr = new WebServiceReference.AppServiceSoapClient();
        }

        private void 网站ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            System.Diagnostics.ProcessStartInfo info = new System.Diagnostics.ProcessStartInfo(LogicLayer.Web);
            System.Diagnostics.Process.Start(LogicLayer.Web); 
        }

        private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            wsr.Logout(LogicLayer.SID);
            Application.Exit();
        }

        private void 注销ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            wsr.Logout(LogicLayer.SID);
            Program.theone.Show();
            this.Close();
        }

        private void 关于ToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void timer_checksession_Tick(object sender, EventArgs e)
        {
            if (wsr.RenewSession(LogicLayer.SID))
            {
                lb_status.Text = "已登录";
            }
            else
            {
                this.Enabled = false;
                MessageBox.Show("Session验证失败，可能是网络中断或服务超时，请重新登录。", "提示");
                Program.theone.Show();
                this.Close();
            }
        }

        private void main_Load(object sender, EventArgs e)
        {
            lb_user.Text = LogicLayer.UID;
            lb_sid.Text = LogicLayer.SID;
            refreshData();
        }



        private void 刷新数据ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            refreshData();
        }


        public void refreshData()
        {
            LogicLayer.dt_person = wsr.GetPersons(LogicLayer.SID);
            LogicLayer.dt_branch = wsr.GetBranchs(LogicLayer.SID);
            
            dataGridView1.DataSource = LogicLayer.dt_person;
            dataGridView2.DataSource = LogicLayer.dt_branch;

            
        }

        protected override void OnClosing(CancelEventArgs e)
        {
            if(Program.theone.Visible == false)
                Program.theone.Close();

            base.OnClosing(e);
        }

        private void delete党员_Click(object sender, EventArgs e)
        {
            DialogResult result = new DialogResult();
            result = MessageBox.Show("您确定要删除选定数据吗？", "警告", MessageBoxButtons.YesNo);
            if (result == DialogResult.No)
            {
                return;                
            }
            foreach (DataGridViewRow dgvr in dataGridView1.SelectedRows)
            {

                try
                {
	                wsr.DeletePerson(LogicLayer.SID, dgvr.Cells[0].Value.ToString());
                }
                catch (System.Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
            this.refreshData();
        }

        private void update党员_Click(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedRows.Count > 1)
            {
                MessageBox.Show("您一次只能修改一个人的信息", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            pb_ws_client.WebServiceReference.person_info_r temp = 
                new pb_ws_client.WebServiceReference.person_info_r();
            temp.ID = dataGridView1.SelectedRows[0].Cells[0].Value.ToString();
            temp.NUMBER = dataGridView1.SelectedRows[0].Cells[1].Value.ToString();
            temp.NAME = dataGridView1.SelectedRows[0].Cells[2].Value.ToString();
            temp.GENDER = dataGridView1.SelectedRows[0].Cells[3].Value.ToString();
            temp.NATIONALITY = dataGridView1.SelectedRows[0].Cells[4].Value.ToString();
            temp.BIRTHDAY = dataGridView1.SelectedRows[0].Cells[5].Value.ToString();
            temp.IDNUMBER = dataGridView1.SelectedRows[0].Cells[6].Value.ToString();
            temp.TELEPHONE = dataGridView1.SelectedRows[0].Cells[7].Value.ToString();
            temp.EMAIL = dataGridView1.SelectedRows[0].Cells[8].Value.ToString();
            temp.PRIORITY_ID = dataGridView1.SelectedRows[0].Cells[9].Value.ToString();
            PersonalInfo PI = new PersonalInfo(wsr, this, temp);
            PI.Show();

        }

        private void add党员_Click(object sender, EventArgs e)
        {
            PersonalInfo PI = new PersonalInfo(wsr, this);
            PI.Show();
        }

        private void add支部_Click(object sender, EventArgs e)
        {
            BranchInfo BI = new BranchInfo(wsr, this);
            BI.Show();
        }

        private void update支部_Click(object sender, EventArgs e)
        {
            if (dataGridView2.SelectedRows.Count > 1)
            {
                MessageBox.Show("您一次只能修改一个支部的信息", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            pb_ws_client.WebServiceReference.branch_info_r temp =
                new pb_ws_client.WebServiceReference.branch_info_r();
            temp.ID = dataGridView2.SelectedRows[0].Cells[0].Value.ToString();
            temp.YEAR = dataGridView2.SelectedRows[0].Cells[1].Value.ToString();
            temp.BR_NAME = dataGridView2.SelectedRows[0].Cells[2].Value.ToString();
            BranchInfo BI = new BranchInfo(wsr, this, temp);
            BI.Show();


        }

        private void delete支部_Click(object sender, EventArgs e)
        {
            DialogResult result = new DialogResult();
            result = MessageBox.Show("您确定要删除选定数据吗？", "警告", MessageBoxButtons.YesNo);
            if (result == DialogResult.No)
            {
                return;
            }
            foreach (DataGridViewRow dgvr in dataGridView2.SelectedRows)
            {

                try
                {
                    wsr.DeleteBranch(LogicLayer.SID, dgvr.Cells[0].Value.ToString());
                }
                catch (System.Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
            this.refreshData();
        }

        private void dataGridView2_SelectionChanged(object sender, EventArgs e)
        {
            this.delete支部.Enabled = true;
            foreach(DataGridViewRow dgvr in dataGridView2.SelectedRows)
                if (dgvr.Cells[0].Value.ToString() == "1")
                {
                    this.delete支部.Enabled = false;
                    break;
                }
        }

        private void 审核帖子ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Post pfm = new Post();
            pfm.Show();
        }

    }



}
